package com.lovemanai.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(filterName = "log",
		urlPatterns = "/*")
public class LogFilter implements Filter {

	//FilterConfig可用于访问Filter的配置信息
	private FilterConfig config;
	
	@Override
	public void destroy() {
		this.config = null;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//获得ServletContext对象，用于记录日志
		ServletContext context = this.config.getServletContext();
		
		System.out.println("开始过滤.....");
		
		//将ServletRequest对象转换成HttpServletRequest对象
		HttpServletRequest hrequest = (HttpServletRequest)request;
		
		System.out.println("已截获用户请求地址：" + hrequest.getServletPath());
		
		//Filter只是进行链式处理，处理完成后再放行
		chain.doFilter(request, response);
		
		System.out.println("过滤结束");
		
		System.out.println("请求被定为到 ：" + hrequest.getRequestURL());
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
	}

}
